package com.DJ;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;


@SpringBootApplication
public class UserApplication {

    private static final Logger log = LoggerFactory.getLogger(UserApplication.class);
    //环境
    private Environment env;
    //程序
    public UserApplication(Environment env) {
        this.env = env;
    }

    public static void main(String[] args) throws UnknownHostException {
        System.out.println("开始启动");
        SpringApplication app = new SpringApplication(UserApplication.class);
        Map<String, Object> defProperties = new HashMap<>();
        defProperties.put("spring.profile.default", "test");
        app.setDefaultProperties(defProperties);
        app.addListeners();

        Environment env = app.run(args).getEnvironment();
        log.info("\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}\n\t" + "External: \thttp://{}:{}\n\t"
                        + "Profile(s): \t{}\n----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("local.server.port", env.getProperty("server.port")),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("local.server.port", env.getProperty("server.port")),
                env.getActiveProfiles());

        String configServerStatus = env.getProperty("configserver.status");
        log.info(
                "\n----------------------------------------------------------\n\t"
                        + "Config Server: \t{}\n----------------------------------------------------------",
                configServerStatus == null ? "Not found or not setup for this application"
                        : configServerStatus);
        System.out.println("启动成功");
    }
}
